package com.weixing.mall.provider.controller.front;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.core.support.auth.MemAuth;
import com.weixing.mall.core.support.auth.MemInfo;
import com.weixing.mall.provider.model.dto.GoodsConcernDto;
import com.weixing.mall.provider.model.vo.GoodsConcernVo;
import com.weixing.mall.provider.service.IGoodsConcernService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author lgc
 * @ClassName FrontGoodsConcernController
 * @Description
 * @date 2019/12/6 15:06
 * @Version 1.0
 */
@RestController
@RequestMapping("front/goodsconcern")
@Api(value = "前端:商品收藏Api",tags = "前端:商品收藏")
public class FrontGoodsConcernController extends BaseController{

    @Autowired
    private IGoodsConcernService iGoodsConcernService;

    @ApiOperation(value = "加入收藏")
    @PostMapping("/save")
    public Result save(@MemInfo MemAuth auth, @RequestBody GoodsConcernDto goodsConcernDto){
        int count = iGoodsConcernService.selectCount(auth.getMid());
        if (count>=100){
            return ResultUtil.error("添加收藏失败,收藏夹商品信息过多!");
        }
        iGoodsConcernService.save(goodsConcernDto,auth.getMid());
        return ResultUtil.success();
    }

    @ApiOperation(value = "[编辑购物车]-移入收藏夹")
    @PostMapping("/saveBatch/{ids}")
    public Result saveBatch(@MemInfo MemAuth auth,@PathVariable List<Long> ids){
//        int count = iGoodsConcernService.selectCount() + ids.length;
//        if (count>=100){
//            return ResultUtil.error("添加收藏夹失败,收藏夹商品信息过多!");
//        }
        iGoodsConcernService.saveBatch(ids,auth.getMid());

        return ResultUtil.success();
    }

    @ApiOperation(value = "我的收藏")
    @GetMapping("")
    public Result getListByMemberId(@MemInfo MemAuth auth) {
        IPage<GoodsConcernVo>  goodsConcernVoList = iGoodsConcernService.selectByMemberId(initMpPage(),auth.getMid());
        return ResultUtil.success(goodsConcernVoList);
    }

    /**
     * 批量删除存在空格错误，还没有找打合适的解决方案，暂时用但删除调通接口
     * @param ids
     * @return
     */
    @ApiOperation(value = "批量删除")
    @PostMapping("/del/{ids}")
    public Result del(@PathVariable String  ids){
        /*String [] tt=ids.split(",");
        try{
          for(String i:tt){
              iGoodsConcernService.removeById(Long.valueOf(i));
          }
        }catch (Exception e){
            e.printStackTrace();
            return  ResultUtil.error();
        }*/
        if(StrUtil.isNotEmpty(ids)){
            iGoodsConcernService.removeByIds(Arrays.asList(ids.split(",")));
        }
        return ResultUtil.success();
    }


 /*   @ApiOperation(value = "根据商品id删除")
    @PostMapping("/del/{id}")
    public Result del(Long id){
       try{
           iGoodsConcernService.del(id);
       }catch (Exception e){
           e.printStackTrace();
           return  ResultUtil.error();
       }
        return ResultUtil.success();
    }*/

    /**
     * 通过商品ID和货品ID来取消收藏
     */
    @ApiOperation(value = "取消收藏")
    @PostMapping("/cancel")
    public Result cancel(@MemInfo MemAuth auth, @RequestBody GoodsConcernDto goodsConcernDto){
        if(auth.getMid() != null && goodsConcernDto.getGoodsId() != null && goodsConcernDto.getProductId() != null){
            iGoodsConcernService.cancel(auth.getMid(),goodsConcernDto.getGoodsId(),goodsConcernDto.getProductId());
            return ResultUtil.success();
        }
        return ResultUtil.error("取消失败");
    }
}
